package top.easyblog.core.support.filter;

import java.util.ArrayList;
import java.util.List;
import java.util.Objects;

/**
 * 默认的事件转换过滤器链
 */
public class DefaultTransformFilterChain implements TransformFilterChain {

    private int pos = 0;
    private final List<TransformFilter> filters = new ArrayList<>();

    @Override
    public void doFilter(TransformContext context) {
        if (this.pos < this.filters.size()) {
            TransformFilter transformFilter = this.filters.get(pos++);
            transformFilter.doFilter(this, context);
        }
    }

    void addTransformFilter(TransformFilter filter) {
        if (Objects.isNull(filter) || filters.contains(filter)) {
            return;
        }

        this.filters.add(filter);
    }

    @Override
    public void release() {
        filters.clear();
        this.pos = 0;
    }

}
